//buffer all maps

var lightnorway = new Image();			lightnorway.src = 'images/map/frontnorway.png';
var lightsweden = new Image();			lightsweden.src = 'images/map/frontsweden.png';
var lightfinland = new Image();			lightfinland.src = 'images/map/frontfinland.png';
var lightsaintpetersburg = new Image(); lightsaintpetersburg.src = 'images/map/frontsaintpetersburg.png';
var lightmoscow = new Image();			lightmoscow.src = 'images/map/frontmoscow.png';
var lightlivonia = new Image();			lightlivonia.src = 'images/map/frontlivonia.png';
var lightprussia = new Image();			lightprussia.src = 'images/map/frontprussia.png';
var lightberlin = new Image();			lightberlin.src = 'images/map/frontberlin.png';
var lightkiel = new Image();			lightkiel.src = 'images/map/frontkiel.png';
var lightdenmark = new Image();			lightdenmark.src = 'images/map/frontdenmark.png';

var widthofwindow = $(window).width();
widthofwindow = widthofwindow-960;
windowsize = widthofwindow/2;

//province, startcoords x, y, neighbors[x]
var sweden			= new Province(470,300,lightsweden,lightnorway,lightdenmark,lightfinland);
var saintpetersburg = new Province(720,250,lightsaintpetersburg,lightfinland,lightmoscow,lightlivonia);
var prussia			= new Province(490,430,lightprussia,lightberlin,lightlivonia);
var norway			= new Province(430,250,lightnorway,lightsweden,lightfinland);
var finland			= new Province(600,220,lightfinland,lightnorway,lightsweden,lightsaintpetersburg);
var denmark			= new Province(410,370,lightdenmark,lightkiel);
var livonia			= new Province(600,350,lightlivonia,lightprussia,lightsaintpetersburg,lightmoscow);
var moscow			= new Province(710,390,lightmoscow,lightlivonia,lightsaintpetersburg);

$(document).ready(function(){
	if($("#canvas").is("canvas")){

		drawMap();

		var eng01 = new Unit("#eng01");	eng01.stickarmy(sweden);
		var fra01 = new Unit("#fra01");	fra01.stickarmy(finland);
		var rus01 = new Unit("#rus01");	rus01.stickarmy(prussia);
	}
});

function Unit(name){
	var province;
	var move = true;
	var click = 0;
	var that = this;
	//add unit to province
	this.stickarmy = function(whatprovince){
		province = whatprovince;
		x = whatprovince.stickx;
		y = whatprovince.sticky;
		$(name).css('left', x).css('top', y);
	}

	$(name).click(function(e){
		//console.debug(click);
		if(click == 1){
			var X = e.pageX - windowsize;
			var Y = e.pageY - 60;
			var data = context.getImageData(X, Y, 1, 1).data;
			if(data[3] == 153){
				$.post('view/templates/findprovince.php', {"x" : X, "y" : Y},
					function(whatprovince){
						console.debug(whatprovince);
						switch(whatprovince){
							//all this should be automated and picked from server. positibly calling a php-script to send all data
							case "sweden":
								that.stickarmy(sweden);
								break;
							case "norway":
								that.stickarmy(norway);
								break;
							case "finland":
								that.stickarmy(finland);
								break;
							case "denmark":
								that.stickarmy(denmark);
								break;
							case "saintpetersburg":
								that.stickarmy(saintpetersburg);
								break;
							case "livonia":
								that.stickarmy(livonia);
								break;
							case "moscow":
								that.stickarmy(moscow);
								break;
							case "out":
								that.stickarmy(province);
								break;
						}
						move = false;
						click = 0;
						context.clearRect(0,0,960,804);
				});
			}
		}else{
			click = 1;
			move = true;
			//console.debug(move);
		//console.debug(click);
		}
		//click = 1;
		$("#canvas").mousemove(function(e){
			if(move == true){
				$(name).css('left', e.pageX).css('top', e.pageY);
			}
		});
		if(province.neighbors.length==1)
			drawMap(province.neighbors[0]);
		if(province.neighbors.length==2)
			drawMap(province.neighbors[0],province.neighbors[1]);
		if(province.neighbors.length==3)
			drawMap(province.neighbors[0],province.neighbors[1],province.neighbors[2]);
		if(province.neighbors.length==4)
			drawMap(province.neighbors[0],province.neighbors[1],province.neighbors[2],province.neighbors[3]);
	});
}

function drawMap(){
	context = document.getElementById('canvas').getContext('2d');
	context.clearRect(0,0,960,804);
	//draw
	for(var i=0;i<arguments.length;i++){
		imgsrc = arguments[i];
		context.drawImage(imgsrc, 0, 0);
		context.drawImage(imgsrc, 0, 0);
	}
}

function Province(x, y){
	this.stickx = (x+windowsize), this.sticky = y; //army position
	this.neighbors = new Array(); //selfnamed
	for(var i=2;i<arguments.length;i++){
		this.neighbors[i-2] = arguments[i];
	}
}
